import { routerRedux } from 'dva/router';
import Cookies from 'js-cookie';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';
import { getPageQuery } from '../utils/utils';
import {
    login,
    addUser,
    queryUser,
    editUser,
    deleteUser,
    addMoney,
    queryTransaction,
    addTransaction,
    editTransaction,
    deleteTransaction,
} from '../services/user';


export default {
    namespace: 'user',

    state: {
        status: false,
        userList: [],
        transactionList: [],
        user: null,
    },
    effects: {
        // 登录
        *login({ payload }, { call, put }) {
            const response = yield call(login, payload);
            if (response && response.obj) {
                // 本地保存用户信息
                Cookies.set('token', response.obj, { path: '/' });
            }
            if (response) {
                // 重新写入权限
                setAuthority(response.currentAuthority ? response.currentAuthority : '1');

                // 重新获取组件
                reloadAuthorized();
                yield put({
                    type: 'changeLoginStatus',
                    payload: { status: 'ok' },
                });
                yield put({
                    type: 'saveUser',
                    payload: { account: payload.user }
                });
                // Login successfully
                const urlParams = new URL(window.location.href);
                const params = getPageQuery();
                let { redirect } = params;
                if (redirect) {
                    const redirectUrlParams = new URL(redirect);
                    if (redirectUrlParams.origin === urlParams.origin) {
                        redirect = redirect.substr(urlParams.origin.length);
                        if (redirect.startsWith('/#')) {
                            redirect = redirect.substr(2);
                        }
                    } else {
                        window.location.href = redirect;
                        return;
                    }
                }
                yield put(routerRedux.replace(redirect || '/'));
            }
        },
        // 登出  暂时只是去掉cookies
        *logout(_, { put }) {
            Cookies.remove('token');
            yield put({
                type: 'changeLoginStatus',
                payload: { status: false, currentAuthority: null },
            });
            reloadAuthorized();
            yield put(routerRedux.replace('/user/login'));
        },
        // 添加用户
        *addUser({ payload }, { call }) {
            const response = yield call(addUser, payload);
            return response;
        },
        // 查询用户
        *queryUser(_, { call, put }) {
            const response = yield call(queryUser);
            yield put({
                type: 'saveUserList',
                payload: !response ? [] : response.obj ? response.obj : []
            });
        },
        // 修改用户
        *editUser({ payload }, { put, call }) {
            const response =  yield call(editUser, payload);
            if (response) {
                yield put({
                    type: 'queryUser'
                });
            }
            return response;
        },
        // 删除用户
        *deleteUser({ payload }, { put, call }) {
            const response = yield call(deleteUser, payload);
            if (response) {
                yield put({
                    type: 'queryUser'
                });
            }
            return response;
        },
        // 充值
        *addMoney({ payload }, { call, put }) {
            const response = yield call(addMoney, payload);
            if (response) {
                yield put({
                    type: 'queryUser'
                });
            }
            return response;
        },
        // 查询交易
        *queryTransaction(_, { put, call }) {
            const response =  yield call(queryTransaction);
            yield put({
                type: 'saveTransactionList',
                payload: !response ? [] : response.obj ? response.obj : []
            });
        },
        // 新增交易
        *addTransaction({ payload }, { call }) {
            const response = yield call(addTransaction, payload);
            return response;
        },
        // 修改交易
        *editTransaction({ payload }, { put, call }) {
            const response = yield call(editTransaction, payload);
            if (response) {
                yield put({
                    type: 'queryTransaction'
                });
            }
            return response;
        },
        // 删除交易
        *deleteTransaction({ payload }, { put, call }) {
            const response = yield call(deleteTransaction, payload);
            if (response) {
                yield put({
                    type: 'queryTransaction'
                });
            }
            return response;
        }
    },

    reducers: {
        changeLoginStatus(state, { payload }) {
            if (payload.currentAuthority) {
                setAuthority(payload.currentAuthority);
            }
            return {
                ...state,
                status: payload.status,
            };
        },
        // 保存登录用户信息
        saveUser(state, { payload }) {
            return {
                ...state,
                user: payload.account
            };
        },
        // 保存查询用户的结果
        saveUserList(state, { payload }) {
            return {
                ...state,
                userList: payload
            };
        },
        // 保存查询交易的结果
        saveTransactionList(state, { payload }) {
            return {
                ...state,
                transactionList: payload
            };
        },
    },
};
